<?php

namespace App\Http\Controllers\Agent;
use App\Models\User;
use App\Service\SysService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Models\GuildUser;
use App\Models\Withdrawal;
use App\Models\WithDraw;
use Illuminate\Support\Facades\DB;


class IndexController extends Controller
{
    public $v = 'agent.index.';
    public $paginate = 15;
    protected $pwdCost = 12;
	
	/**
	 * 首页
	 * @return type
	 */
    public function index()
    {
        $user = Auth::guard('agent')->user();
        return view($this->v . 'agent_index', ['user' => $user]);
    }
	/**
	 * 公告
	 * @return type
	 */
    public function aunnoung(Request $request)
    {
		$keyword = $request->query('keyword');
		$builder = new \App\Models\Announ();
		$query =  $builder
			->when($keyword, function ($query) use ($keyword) {
				return $query->where('title', 'like', '%'.$keyword.'%')->orWhere('content', 'like', '%'.$keyword.'%'); 
		});		
		$aunnoung = $query->orderBy('id', 'desc')->paginate($this->paginate);
        return view($this->v . 'aunnoung', compact('aunnoung'));		
    }
	/**
	 * 公告详情
	 * @return type
	 */
	public function aunndetails(Request $request, $id) {
		$details = (new \App\Models\Announ())->where('id', $id)->first();
		return view($this->v . 'aunndetails', compact('details'));
	}

	/**
     * 修改密码
     * @param  Request  $request
     * @return Response
     * @author zhou
     */
    public function edit_save(Request $request, GuildUser $guildUser)
    {
        $agent = Auth::guard('agent')->user();
        $post = $request->post();
        $oldpwd = $post['oldpwd'];
        $newpwd = $post['newpwd'];
        $res = password_verify($oldpwd, $agent->password);
        if ($res === false) return ajaxError("原密码输入错误");
        $newpassword = password_hash($newpwd, PASSWORD_DEFAULT, ['cost' => $this->pwdCost]);
        $updateId = $guildUser->where(["id" => $agent->id])->update(["password"=>$newpassword]);
        if (!$updateId) return ajaxError("修改密码错误");
        return ajaxSuccess();
    }
   
    /**
     * 进入修改密码界面
     * @param  Request  $request
     * @return Response
     * @author zhou
     */
    public function changePassword()
    {
        return view($this->v . 'change_password');
    }
    /**
     * 修改密码postr
     * @param  Request  $request
     * @return Response
     * @author zhou
     */
    public function editPassword(Request $request, GuildUser $guildUser)
    {
        $agent = Auth::guard('agent')->user();
        $password = $request->input('password');
        $res = password_verify($password, $agent->password);
        $newpassword= $request->input('newpassword');
        if ($res === false)   return  ajaxError("原密码错误");
        $newPassword=password_hash($newpassword, PASSWORD_DEFAULT, ['cost' => $this->pwdCost]);
        $updateId = $guildUser ->where('id',$agent->id)->update(["password"=>$newPassword]);
        if (!$updateId) return ajaxError("修改密码错误");
        return ajaxSuccess();
    }

    public function flushCache()
    {
        $user = Auth::guard('agent')->user();
        $user->getUserMenu(true);
        $user->getUserRules(true);
        (new SysService())->getSystemInfo(true);
        return ajaxSuccess();
    }

    public function cleanCache()
    {
        (new GuildUser())->cleanUserData();
        (new SysService())->cleanSysInfo();
        return ajaxSuccess();
    }

    public function forbidden()
    {
        return view($this->v . 'forbidden');
    }

    public function first()
    {
        $sysinfo = (new SysService())->getSystemInfo();
        return view($this->v . 'first', compact('sysinfo'));
    }
	/**
	 * 上传客服二维图片
	 */
	public function agupload(Request $request) {
		$file = request()->file('file');
		$type = $request->input('type');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'未上传图片')));
		}
		// 获取文件后缀
		$temp = explode(".", $_FILES["file"]["name"]);
		$extension = end($temp);
		// 判断文件是否合法
		if(!in_array($extension,array("gif","jpeg","jpg","png"))){
			exit(json_encode(array('code'=>1,'msg'=>'上传图片不合法')));
		}
		//检测目录是否存在
		$dirpath = 'storage/'.$type.'/';
		if (!is_dir($dirpath)){
			mkdir(iconv("UTF-8", "GBK", $dirpath), 0777, true);
		}
		// 文件是否上传成功
	   if ($file->isValid()) {		  
		   // 获取文件相关信息			
			$newName = rand(100, 999).date('i').date('s').".".$extension;	
			$path = $file->move($dirpath, $newName);
			$namePath = '/'.$dirpath . $newName;
			exit(json_encode(array('code'=>0, 'msg'=> $namePath)));
	   }
	   exit(json_encode(array('code'=>1,'msg'=>'上传失败')));
	}

}
